2027 Method
Changes 0
M

ElementTransformUtils.CopyElements

Description:
Copies a set of elements from source document to destination document.
Remarks:

Copies are placed at their respective original locations or locations specified by the optional transformation.

This method can be used for copying non-view specific elements only. For copying view-specific elements, use the view-specific form of the CopyElements method.

The destination document can be the same as the source document.

This method performs rehosting of elements where applicable.

Overloads (3):
CopyElements(Document,ICollection[ElementId],Document,Transform,CopyPasteOptions)
public static ICollection<ElementId> CopyElements(
	Document sourceDocument,
	ICollection<ElementId> elementsToCopy,
	Document destinationDocument,
	Transform transform,
	CopyPasteOptions options
)
  • sourceDocument
    The document that contains the elements to copy.
  • ICollection<ElementId>
    elementsToCopy
    The set of elements to copy.
  • destinationDocument
    The destination document to paste the elements into.
  • transform
    The transform for the new elements. Can be null if no transform is required.
  • options
    Optional settings. Can be null if default settings should be used.
Return Value ICollection<ElementId> The ids of the newly created copied elements.
// Copy wall element.
public void CopyWall(Autodesk.Revit.DB.Document sourceDocument, Autodesk.Revit.DB.Document destinationDocument, Autodesk.Revit.DB.Wall wall)
{
   Transform transform = Transform.CreateTranslation(new XYZ(150, 150, 0));
   CopyPasteOptions options = new CopyPasteOptions();

   ElementTransformUtils.CopyElements(sourceDocument, new List<ElementId> { wall.Id }, destinationDocument, transform, options);
}

// The method copies existing Sketch curves inside of the Floor sketch.
public void CopySketchMembersInsideOfSketch(Autodesk.Revit.DB.Document document, Autodesk.Revit.DB.Floor floor)
{
   var floorSketch = document.GetElement(floor.SketchId) as Sketch;

   // Start Sketch edit mode to copy Sketch members inside of the Sketch
   SketchEditScope sketchScope = new SketchEditScope(document, "Edit floor sketch");
   sketchScope.Start(floor.SketchId);

   // Start transaction to be able to modify the document
   using (Transaction tr = new Transaction(document, "Copy sketch members"))
   {
      tr.Start();

      var transform = Transform.CreateTranslation(new XYZ(150, 150, 0));
      var copiedIds = ElementTransformUtils.CopyElements(
         document, floorSketch.GetAllElements(), document, transform, new CopyPasteOptions());

      tr.Commit();
   }

   sketchScope.Commit(new CustomFailuresPreprocessor());
}

// The method copies Sketch members from the Floor to the Ceiling Sketch.
public void CopySketchMembersBetweenSketch(Autodesk.Revit.DB.Floor floor, Autodesk.Revit.DB.Ceiling ceiling)
{
   var ceilingSketch = ceiling.Document.GetElement(ceiling.SketchId) as Sketch;
   var floorSketch = floor.Document.GetElement(floor.SketchId) as Sketch;

   // Sketch members can be copied only between parallel Sketches.
   if (!MathComparisonUtils.IsAlmostEqual(
      Math.Abs(floorSketch.SketchPlane.GetPlane().Normal.DotProduct(ceilingSketch.SketchPlane.GetPlane().Normal)), 1.0))
   {
      return;
   }

   // Start Sketch edit mode, for the ceiling sketch, to copy and paste floor Sketch members to the ceiling
   SketchEditScope sketchScope = new SketchEditScope(ceiling.Document, "Edit ceiling sketch");
   sketchScope.Start(ceiling.SketchId);

   // Start transaction to be able to modify the document
   using (Transaction tr = new Transaction(ceiling.Document, "Copy sketch members"))
   {
      tr.Start();

      var transform = Transform.CreateTranslation(new XYZ(150, 150, 0));
      var copiedIds = ElementTransformUtils.CopyElements(
         floor.Document, floorSketch.GetAllElements(), ceiling.Document, transform, new CopyPasteOptions());

      tr.Commit();
   }

   sketchScope.Commit(new CustomFailuresPreprocessor());
}

// The method copies ModelCurves from the document to the Sketch.
public void CopyModelCurvesFromDocumentToSketch(Autodesk.Revit.DB.Sketch sketch, List<ElementId> modelCurveIds)
{
   // Start Sketch edit mode to insert ModelCurves into the Floor Sketch.
   SketchEditScope sketchScope = new SketchEditScope(sketch.Document, "Edit floor sketch");
   sketchScope.Start(sketch.Id);

   // Start transaction to be able to modify the document
   using (Transaction tr = new Transaction(sketch.Document, "Copy sketch members"))
   {
      tr.Start();

      var transform = Transform.CreateTranslation(new XYZ(150, 150, 0));
      var copiedIds = ElementTransformUtils.CopyElements(
         sketch.Document, modelCurveIds, sketch.Document, transform, new CopyPasteOptions());

      tr.Commit();
   }

   // Sketch curves should not have open loops or intersections to be successfully committed.
   sketchScope.Commit(new CustomFailuresPreprocessor());
}
  • The given element id set is empty. -or- One or more elements in elementsToCopy do not exist in the document. -or- Some of the elements cannot be copied, because they are view-specific. -or- The input set of elements contains Sketch members along with other elements or there is no active Sketch edit mode.
  • A non-optional argument was null
  • It is not allowed to copy Sketch members between non-parallel sketches. -or- The elements cannot be copied.
  • User cancelled the operation.